package cloud.seat.order.controller;


import cloud.seat.order.client.AccountClient;
import cloud.seat.order.client.WarehouseClient;
//import cloud.seat.order.repository.OrderProducer;
//import cloud.seat.order.sg.OrderEvent;
import cloud.seata.api.TccAccountService;
import cloud.seata.api.TccWarehouseService;
import org.apache.dubbo.config.annotation.DubboReference;
//import org.apache.rocketmq.common.message.MessageConst;
//import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.seata.core.context.RootContext;
import org.apache.seata.rm.tcc.api.BusinessActionContext;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
//import org.springframework.messaging.Message;
//import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;

@RestController
public class OrderController {

    @DubboReference
    private TccAccountService tccAccountService;

    @DubboReference
    private TccWarehouseService tccWarehouseService;

    @Autowired
    private AccountClient accountClient;

    @Autowired
    private WarehouseClient warehouseClient;

//    @Autowired
//    private StreamBridge streamBridge;

    @PostMapping("/order/make")
    @GlobalTransactional
    public ResponseEntity<Boolean> make() {

        String xid = RootContext.getXID();
        BusinessActionContext bac = new BusinessActionContext();
        bac.setXid(xid);
        accountClient.prepare(bac);

        warehouseClient.prepare(bac);

        return ResponseEntity.status(HttpStatus.OK).body(Boolean.TRUE);
    }


    @PostMapping("/order/dd")
    @GlobalTransactional
    public ResponseEntity<Boolean> dd() throws Exception {

        String xid = RootContext.getXID();
        BusinessActionContext bac = new BusinessActionContext();
        bac.setXid(xid);
        HashMap<String, Object> map = new HashMap<>();
        map.put("productId", 1234L);
        map.put("frozen", 12456);
        bac.setActionContext(map);
        tccAccountService.prepare(bac);

        tccWarehouseService.prepare(bac, 1234L, 12456);

        return ResponseEntity.status(HttpStatus.OK).body(Boolean.TRUE);
    }

//    @Autowired
//    OrderProducer orderProducer;
//
//    @Autowired
//    private RocketMQTemplate rocketMQTemplate;

//    @PostMapping("/order/mq")
//    public ResponseEntity<Boolean> ddasdg() throws Exception {

//        // 设置延迟级别（例如 delayLevel=4 表示延迟 30s）
//        Message<String> message = MessageBuilder.withPayload("asdgagh")
//                .setHeader("DELAY", 2)  // 关键：通过 Header 指定延迟级别
//                .build();
//        streamBridge.send("order-output", message);

//        OrderEvent orderEvent = new OrderEvent();
//        orderEvent.setOrderId("sdga");
//        orderEvent.setType("sg");
//        orderEvent.setAmount(new BigDecimal(123));
//
//        orderProducer.sendOrderEvent(orderEvent);



//        Message<String> message = MessageBuilder.withPayload("sdgasdg")
//                .setHeader(MessageConst.PROPERTY_KEYS, "sdgasdg") // 设置消息Key（追踪用）
//                .build();
//        rocketMQTemplate.syncSend("ORDER_TOPIC:payTag", message); // TOPIC:Tag格式:cite
//        return ResponseEntity.status(HttpStatus.OK).body(Boolean.TRUE);
//    }
}
